# from lxml import etree
# import requests
# import csv
# import time
# def func(item):
#     with open('D:/allhouse.csv','a',encoding="utf-8")as f:
#         writer = csv.writer(f)
#         writer.writerow(item)
#         f.close()
#
# if __name__ =='__main__':
#     headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36"}
#     url = "https://shenzhen.qfang.com/sale/f"
#
#     for one in range(1,100):
#         one=str(one)
#         urls=url+one
#         req=requests.get(urls,headers=headers)
#         time.sleep(1)
#         html = etree.HTML(req.text) #将页面整个html赋值给element对象
#         xiaoqulist=html.xpath('//*[@id="cycleListings"]/ul/li')
#         count = 0
#         try:
#             for xiaoqu in xiaoqulist:
#
#                 housename=xiaoqu.xpath("div[2]/div[1]/a/text()")
#                 price=xiaoqu.xpath("div[3]/p[1]/span[2]/text()")
#                 intprice=price[0]
#
#
#                 Referenceprice=xiaoqu.xpath("div[3]/p[2]/text()")#政府参考价
#
#                 housemeter=xiaoqu.xpath("div[2]/div[2]/p[1]/text()")
#                 housebig=xiaoqu.xpath("div[2]/div[2]/p[2]/text()")
#
#                 region = xiaoqu.xpath("div[2]/div[3]/div/text()")#原地区
#                 newregion=[''.join([i.strip() for i in price.strip().split('\n')]) for price in region][0]#处理之后的地区
#
#                 address = xiaoqu.xpath("div[2]/div[3]/div/a/text()")#取具体地址
#                 newaddress=[''.join([i.strip() for i in price.strip().split('\n')]) for price in address][0]#处理之后的新具体地址
#
#                 alladdress = newregion + newaddress  # 处理之后的地区+具体地址(总地址)
#
#                 item = [housename, intprice,Referenceprice, alladdress, housemeter, housebig]
#                 print(f'名称:{housename}\n 总价:{intprice}万\n 政府参考价：{Referenceprice}\n 地址：{alladdress}\n 房子类型;{housemeter}\n 房子大小：{housebig}\n')
#                 count=count+1
#                 print(f"-------第{one}页第{count}房子-------- ")
#                 func(item)
#
#
#         except Exception as e:
#             print(f"Exception found:{e}")









